package com.credithc.signature.job.controller;

import com.alibaba.fastjson.JSONObject;
import com.credithc.signature.job.client.QyWxClient;
import com.credithc.signature.job.job.HomeworkJob;
import com.credithc.signature.job.job.ReprocessJob;
import com.credithc.signature.job.job.StatisticsReqJob;
import com.credithc.signature.job.ro.EmailRo;
import com.credithc.signature.job.ro.WxRo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉28⽇<br/>
 * @version：v1.0.0<br>
 */
@RestController
@RequestMapping("/api/job/v1")
public class JobDemo {

    @Autowired
    ReprocessJob reprocessJob;

    @Autowired
    private QyWxClient client;

    @Value("${qywx.key}")
    private String key;

    @Autowired
    HomeworkJob jobHandler;

    @Autowired
    StatisticsReqJob statisticsReqJob;

    @GetMapping("/reExecuteJobHandler")
    public String healthCheck() throws Exception {
        reprocessJob.reExecuteJobHandler();
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "检测成功");
        return object.toJSONString();
    }

    @GetMapping("/send")
    public String send() throws Exception {
        String[] arr = new String[]{"hujingbo20170915@credithc.com"};
        WxRo wxRo = new WxRo(key, "test", "\"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">恭喜你抽中iPhone 7一台，领奖码：xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>\"", "url", Arrays.asList(arr));
        client.sendWxText(wxRo);
        client.sendWxCard(wxRo);

        EmailRo emailRo = new EmailRo(key, "测试邮件", "<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">恭喜你抽中iPhone 7一台，领奖码：xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>", Arrays.asList(arr));
        client.sendEmail(emailRo);
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "发送成功");
        return object.toJSONString();
    }

    @GetMapping("/homework")
    public String homework() throws Exception {
        jobHandler.homeworkJob();
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "发送成功");
        return object.toJSONString();
    }

    @GetMapping("/statisticsRequestFailHandler")
    public String statisticsRequestFailHandler() throws Exception {
        statisticsReqJob.statisticsRequestFailHandler();
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "成功");
        return object.toJSONString();
    }

    @GetMapping("/statisticsRequestTimeHandler")
    public String statisticsRequestTimeHandler() throws Exception {
        statisticsReqJob.statisticsRequestTimeHandler();
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "成功");
        return object.toJSONString();
    }

    @GetMapping("/statisticsRequestDayFailHandler")
    public String statisticsRequestDayFailHandler() throws Exception {
        statisticsReqJob.statisticsRequestDayFailHandler();
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "成功");
        return object.toJSONString();
    }
}
